<!--
 * @Author: liuyonghu liuyonghu.com
 * @Date: 2025-02-20 10:13:31
 * @LastEditors: liuyonghu liuyonghu.com
 * @LastEditTime: 2025-02-20 10:30:20
 * @FilePath: /demos/vue3/v-for.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            box-sizing: border-box;
            padding: 0;
            margin: 0;
        }
    </style>
</head>

<body>
    <div id="app" style="width: 100vw; height: 100vh;padding: 10vw;">

        <div>

            <pre>{{ resText }}</pre>
            <br />
            <hr /> <br />
            <my-li v-for="(item,index) in arr" :key="item.text + index" v-model="item" :index="index"></my-li>
        </div>
    </div>

    <script type="text/x-template" id="my-template">
        <div>
            <div> {{ resIndex }}</div>
            <input type="text" >
        </div>
    </script>
    <script>
        /**
         * ### 七、 vue3 响应式数据在 v-for 中无法更新
         * */
        window.onload = () => {


            const app = Vue.createApp({
                data() {
                    return {
                        arr: [{ text: 1 }, { text: 2 }]
                    }
                },
                computed: {
                    resText() {
                        return JSON.stringify(this.arr, null, '\t')
                    }
                }
            });

            app.component('MyLi', {
                template: '#my-template',
                setup(props) {
                    const model = Vue.defineModel();

                    const resIndex = Vue.computed(() => props.index + 1)
                    return {
                        model,
                        resIndex
                    }
                }
            });

            app.mount('#app');
        }
    </script>
    <script src="https://cdn.jsdelivr.net/npm/vue@3.5.16/dist/vue.global.min.js"></script>
</body>

</html>